package com.example.administrator.exkotlin.contentprovider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri

/**
 * Author by lp,on 2020/7/23/023,15:34.
 */
class MyProvider : ContentProvider() {
    private val tableDir = 0
    private val tableItem = 1
    private val table2Dir = 3
    private val table2item = 4
    //懒加载
    private val uriMatcher by lazy {
        val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)
        //     table1/#：表示table1表中任意一行 用id=?来做选择
        uriMatcher.addURI("com.test.lp.provider", "table1", tableDir)
        uriMatcher.addURI("com.test.lp.provider", "table1/#", tableDir)
        uriMatcher.addURI("com.test.lp.provider", "table2", tableDir)
        uriMatcher.addURI("com.test.lp.provider", "table2/#", tableDir)
        uriMatcher
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        return null
    }

    override fun query(uri: Uri, projection: Array<String>?, selection: String?, selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
        return null
    }

    override fun onCreate(): Boolean {
        return true
    }

    override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
        return 0
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        return 0
    }

    /**
     * MIME类型
     * 必须以 vnd.开头
     * 中间 如果是以URI路径结尾的则为 android.cursor.dir/
     * 如果是以id结尾的 则为 android.cursor.item/
     * 最后拼接 vnd.<authority>.<path>
     */

    override fun getType(uri: Uri) = when (uriMatcher.match(uri)) {
        tableDir -> "vnd.android.cursor.dir/vnd.com.test.lp.provider.teble1"
        tableItem -> "vnd.android.cursor.item/vnd.com.test.lp.provider.teble1"
        table2Dir -> "vnd.android.cursor.dir/vnd.com.test.lp.provider.teble2"
        table2item -> "vnd.android.cursor.item/vnd.com.test.lp.provider.teble2"
        else -> null
    }
}